import format from '@dfeidao/atom-web/dt/format';
import get from '@dfeidao/atom-web/global/get';
import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';

import nodejs from '@dfeidao/atom-web/msg/nodejs';
import render1 from '@dfeidao/atom-web/render/render';
import p001 from './p001';
import p003 from './p003';
import p004 from './p004';

export default async function a001(fd: IFeidaoAiBrowserComponent) {
	// todo
	// 点击p001的查询按钮触发
	const a1 = get('staff_no');
	// 通知标题
	const inform_title1 = fd.data.node.querySelector<HTMLInputElement>('[data-id="inform_title"]').value;
	// 时间
	const receive_time1 = fd.data.node.querySelector<HTMLInputElement>('[data-id="receive_time"]').value;
	const receive_time_end1 = fd.data.node.querySelector<HTMLInputElement>('[data-id="receive_time_end"]').value;
	// 当没有输入日期时给他定义一个时间戳
	let receive_time2 = 10000;
	if (receive_time1 !== "") {
		receive_time2 = new Date(receive_time1).getTime();
	}
	let receive_time_end2 = 10000000000000000;
	if (receive_time_end1 !== "") {
		receive_time_end2 = new Date(receive_time_end1).getTime();
	}
	// 查询服务
	const res1 = await nodejs<{
		res: Array<{
			notice_receive_no: string, inform_no: string, inform_title: string, inform_content: string,
			receive_time: number, sponso_no: string, sponsor: string
		}>; res1: number
	}>('notification-notice/zj-000001/s004',
		{
			filter: {
				pn: 1,
				a : a1 ,
				inform_title : inform_title1,
				receive_time: receive_time2,
				receive_time_end: receive_time_end2
			}
		});
	const arr1 = Array();
	// 获取查询的总条数
	const num = res1.res1[0].inform_title;
	// 获取编号
	for (let i = 0; i < num; i++) {
		arr1.push(res1.res[i].inform_no);
	}
	// 转换日期格式
	const newres1 = res1.res.map((n) => {
		return { ...n, receive_time: format(n.receive_time, 2) };
	});
	// 查询接收人
	const res3 = await nodejs<{ dt: Array<{ inform_no: string, sponso_no: string, sponsor: string }> }>('notification-notice/zj-000001/s003',
		{
			filter: {
				arr : arr1
			}
		});
	// 吧接收人和总数据合并成一个数据
	const newarr = newres1.map((n) => {
		for (let i = 0; i < res3.dt.length; i++) {
			if (n.inform_no === res3.dt[i].inform_no) {
				n.sponso_no = res3.dt[i].sponso_no;
					n.sponsor = res3.dt[i].sponsor;
				return n;
			}
		}
		n.sponsor = "";
		return n;
	});
	const dtPn1 = fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="p005"]');
	dtPn1.style.display = 'none';
	const dtData = fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="p001"]');
	const dtNoData = fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="p003"]');
	const dtPn = fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="p004"]');
	if (newres1.length > 0) {
		render1(fd, newres1, p001, "p001", "inner");
		dtNoData.style.display = 'none';
		dtData.style.display = 'block';

		if (num > 20) {
			render1(fd, newarr, p004, 'p004', "inner");
			dtPn.style.display = 'none';
		}
	} else {
		render1(fd, "", p003, 'p003', 'inner');
		dtNoData.style.display = 'block';
		dtData.style.display = 'none';
		dtPn.style.display = 'none';
	}
}
